package com.qianwang.shopmsg.service.course.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qianwang.shopmsg.common.Page;
import com.qianwang.shopmsg.entity.Course;
import com.qianwang.shopmsg.exception.ParamValidateException;
import com.qianwang.shopmsg.param.CourseCreateParam;
import com.qianwang.shopmsg.param.CourseListParam;
import com.qianwang.shopmsg.repository.CourseMsgDao;
import com.qianwang.shopmsg.service.course.CourseMsgService;
import com.qianwang.shopmsg.util.OvalUtil;

@Service("courseMsgService")
public class CourseMsgServiceImpl implements CourseMsgService {

	@Resource(name="courseMsgDao")
	private CourseMsgDao courseMsgDao;
	
	@Override
	public int countCourse(CourseListParam listParam) {
		Map<String,Object> param = getParam(listParam);
		
		return courseMsgDao.countCourse(param);
	}
	
	@Override
	public void addCourse(Course course) {
		courseMsgDao.addCourse(course);
	}
	
	@Override
	public List<Course> listCourseInfos(Page pg,CourseListParam listParam) {
		Map<String,Object> param = getParam(listParam);
		param.put("pageSize", pg.getPageSize());  
		param.put("pageIndex", pg.getPageIndex());
		
		return courseMsgDao.listCourseInfos(param);
	}
	
	/**
	 * 拼接查询参数
	 * @param listParam
	 * @return
	 */
	private Map<String,Object> getParam(CourseListParam listParam){
		Map<String,Object> param = new HashMap<String, Object>();
		if(null == listParam){
			return param;
		}
		if(StringUtils.isNotBlank(listParam.getStartTime())){
			param.put("startTime", listParam.getStartTime());
		}
		if(StringUtils.isNotBlank(listParam.getEndTime())){
			param.put("endTime", listParam.getEndTime());
		}
		if(StringUtils.isNotBlank(listParam.getCourseName())){
			param.put("courseName", listParam.getCourseName());
		}
		if(StringUtils.isNotBlank(listParam.getTeacherName())){
			param.put("teacherName", listParam.getTeacherName());
		}
		return param;
	}
	
	@Override
	@Transactional(rollbackFor=Throwable.class)
	public void saveCourse(CourseCreateParam courseParam) throws ParamValidateException {
		
		OvalUtil.validate(courseParam);
		
		 Course courseOld = courseMsgDao.loadCourse(courseParam.getCourseId());
		
		if(courseOld != null){
			updateStudentSign(courseParam,courseOld);
		}else{
			createStudentSign(courseParam);
		}
	}
	
	/**
	 * update操作
	 * @param signParam
	 * @param studentInfo 
	 */
	private void updateStudentSign(CourseCreateParam courseParam,Course courseOld){
		BeanUtils.copyProperties(courseParam, courseOld);
		//update 学生信息
		courseMsgDao.updateCourse(courseOld);
	}
	
	/**
	 * create操作
	 * @param signParam
	 */
	private void createStudentSign(CourseCreateParam courseParam){
		Course course = new Course();
		BeanUtils.copyProperties(courseParam, course);
		
		courseMsgDao.addCourse(course);
	}
	
	@Override
	@Transactional(rollbackFor=Throwable.class)
	public void deleteCourse(long id) throws ParamValidateException {
		Course course = courseMsgDao.loadCourse(id);
		if(null == course){
			throw new ParamValidateException("信息不存在，请稍后重试！");
		}
		courseMsgDao.deleteCourseById(id);
	}
	
	@Override
	public Course loadCourseById(long id) throws ParamValidateException {
		Course view = courseMsgDao.loadCourse(id);
		if(null == view){
			throw new ParamValidateException("信息不存在，请稍后重试！");
		}
		return view;
	}
	
}
